package io.xiaojl.xj2ee.modules.sys.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.xiaojl.xj2ee.common.kit.PageWrapper;
import io.xiaojl.xj2ee.modules.sys.entity.SysDeptEntity;
import io.xiaojl.xj2ee.modules.sys.entity.SysRoleEntity;
import io.xiaojl.xj2ee.modules.sys.mapper.SysRoleEntityMapper;
import io.xiaojl.xj2ee.modules.sys.service.SysDeptService;
import io.xiaojl.xj2ee.modules.sys.service.SysRoleDeptService;
import io.xiaojl.xj2ee.modules.sys.service.SysRoleMenuService;
import io.xiaojl.xj2ee.modules.sys.service.SysRoleService;
import io.xiaojl.xj2ee.modules.sys.service.SysUserRoleService;


@Service("sysRoleService")
public class SysRoleServiceImpl implements SysRoleService {

	@Autowired
	private SysRoleMenuService sysRoleMenuService;
	@Autowired
	private SysRoleDeptService sysRoleDeptService;
	@Autowired
	private SysUserRoleService sysUserRoleService;
	@Autowired
	private SysDeptService sysDeptService;
	@Autowired
	private SysRoleEntityMapper sysRoleMapper;

	@Override
//	@DataFilter(subDept = true, user = false)
	public PageWrapper<SysRoleEntity> queryPage(Map<String, Object> params) {
		PageWrapper<SysRoleEntity> pw = new PageWrapper<SysRoleEntity>(params);
		PageHelper.startPage(pw.getCurrPage(), pw.getPageSize(), pw.getOrderBy());
		
		List<SysRoleEntity> list = sysRoleMapper.selectListByPage(pw.getCondition());
		PageInfo<SysRoleEntity> page = new PageInfo<>(list);
		
		for(SysRoleEntity sysRoleEntity : page.getList()){
			SysDeptEntity sysDeptEntity = sysDeptService.selectById(sysRoleEntity.getDeptId());
			if(sysDeptEntity != null){
				sysRoleEntity.setDeptName(sysDeptEntity.getName());
			}
		}
		
		pw.setList(page.getList());
		pw.setTotalCount(Integer.parseInt(page.getTotal()+""));
		pw.setTotalPage(page.getPages());
        return pw;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void save(SysRoleEntity role) {
		role.setCreateTime(new Date());
		sysRoleMapper.insertSelective(role);

		//保存角色与菜单关系
		sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());

		//保存角色与部门关系
		sysRoleDeptService.saveOrUpdate(role.getRoleId(), role.getDeptIdList());
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void update(SysRoleEntity role) {
		sysRoleMapper.updateByPrimaryKeySelective(role);

		//更新角色与菜单关系
		sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());

		//保存角色与部门关系
		sysRoleDeptService.saveOrUpdate(role.getRoleId(), role.getDeptIdList());
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteBatch(Long[] roleIds) {
		//删除角色
		sysRoleMapper.deleteBatchIds(roleIds);

		//删除角色与菜单关联
		sysRoleMenuService.deleteBatch(roleIds);

		//删除角色与部门关联
		sysRoleDeptService.deleteBatch(roleIds);

		//删除角色与用户关联
		sysUserRoleService.deleteBatch(roleIds);
	}

	@Override
	public List<SysRoleEntity> selectList(Map<String, Object> condition) {
		return sysRoleMapper.selectList(condition);
	}

	@Override
	public SysRoleEntity selectById(Long roleId) {
		return sysRoleMapper.selectByPrimaryKey(roleId);
	}
}
